package org.meb.speedway.process.sf;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.BufferOverflowException;
import java.nio.IntBuffer;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class SFEventsIds {

	private static final String EVENTS_FILE_NAME = "sf-events-ids.csv";
	private final int CHUNK = 10;
	private IntBuffer nonSpeedwayIds;
	private IntBuffer speedwayIds;

	public SFEventsIds() {
		nonSpeedwayIds = IntBuffer.allocate(CHUNK);
		speedwayIds = IntBuffer.allocate(CHUNK);
	}

	public void read() throws IOException {
		FileInputStream stream;
		try {
			stream = new FileInputStream(EVENTS_FILE_NAME);
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
			try {
				String line = reader.readLine();
				if (line != null) {
					nonSpeedwayIds = fillIds(line);
				}
				line = reader.readLine();
				if (line != null) {
					speedwayIds = fillIds(reader.readLine());
				}
			} finally {
				if (reader != null) {
					reader.close();
				}
			}
		} catch (Exception e) {
			System.out.println("File doesnot exists: " + EVENTS_FILE_NAME);
		}
	}

	public void write() throws IOException {
		FileOutputStream stream = new FileOutputStream(EVENTS_FILE_NAME);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream));
		int[] arr = new int[nonSpeedwayIds.position()];
		nonSpeedwayIds.get(arr, 0, nonSpeedwayIds.position());
		writer.write(StringUtils.join(ArrayUtils.toObject(arr), ";"));
		writer.newLine();
		arr = new int[speedwayIds.position()];
		speedwayIds.get(arr, 0, speedwayIds.position());
		writer.write(StringUtils.join(ArrayUtils.toObject(arr), ";"));
		writer.flush();
		writer.close();
	}

	public void addNonSpeedwayId(int id) {
		nonSpeedwayIds = addId(nonSpeedwayIds, id);
	}

	public void addSpeedwayId(int id) {
		speedwayIds = addId(speedwayIds, id);
	}

	private IntBuffer fillIds(String idsLine) {
		String[] idsStrings = idsLine.split(";");
		IntBuffer ids = IntBuffer.allocate((int) (idsStrings.length / CHUNK) * (CHUNK + 1));
		for (String idString : idsStrings) {
			try {
				int id = Integer.parseInt(idString);
				ids = addId(ids, id);
			} catch (NumberFormatException e) {
				System.out.println("Unable to parse id: " + idString);
			}
		}
		return ids;
	}

	private IntBuffer addId(IntBuffer ids, int id) {
		try {
			ids.put(id);
		} catch (BufferOverflowException e) {
			ids = IntBuffer.allocate(ids.capacity() + CHUNK).put(ids.array());
			ids.put(id);
		}
		return ids;
	}

	public boolean containsNonSpeedwayId(int id) {
		return ArrayUtils.contains(nonSpeedwayIds.array(), id);
	}

	public boolean containsSpeedwayId(int id) {
		return ArrayUtils.contains(speedwayIds.array(), id);
	}
}
